package com.android.fragmentdemo.thread10

import org.junit.Test
import java.util.concurrent.locks.ReentrantLock

class Demo10 {
    @Test
    fun testRunnable() {

        val reentrantLock = ReentrantLock()

        val condition = reentrantLock.newCondition()

        Thread(
            {
              reentrantLock.lock()
              try {
                  println("${Thread.currentThread().name} come in")
                  try {
                      condition.await()
                  } catch (ex: InterruptedException) {
                      ex.printStackTrace()
                  }
                  println("${Thread.currentThread().name} 被唤醒")
              }  finally {
                  reentrantLock.unlock()
              }
            },
            "name0"
        ).start()

        Thread(
            {
                reentrantLock.lock()
                try {
                    condition.signal()
                    println("${Thread.currentThread().name} 通知")
                } catch (ex: InterruptedException) {
                    ex.printStackTrace()
                } finally {
                    reentrantLock.unlock()
                }
            },
            "name1"
        ).start()

        Thread.sleep(5 * 1000)
    }
}